set(KYBER_SRCS kex.c kem.c indcpa.c polyvec.c poly.c ntt.c cbd.c reduce.c verify.c)
set(KYBER_FIPS202_SRCS ${KYBER_SRCS} symmetric-shake.c)
set(KYBER_NINETIES_SRCS ${KYBER_SRCS} symmetric-aes.c)
set(FIPS202_SRCS fips202.c)
set(AES256CTR_SRCS aes256ctr.c)
set(SHA2_SRCS sha256.c sha512.c)
set(TEST_KYBER_SRCS test_kyber.c randombytes.c)
set(TEST_KEX_SRCS test_kex.c randombytes.c)
set(TEST_VECTORS_SRCS test_vectors.c)
set(TEST_SPEED_SRCS test_speed.c speed_print.c cpucycles.c randombytes.c)

if(MSVC)
  add_compile_options(/nologo /O2 /W4 /wd4146 /wd4244)
else()
  add_compile_options(-Wall -Wextra -Wpedantic -Werror)
  add_compile_options(-Wmissing-prototypes -Wredundant-decls -Wshadow -Wpointer-arith)
  add_compile_options(-O3 -fomit-frame-pointer)
endif()

add_library(fips202_ref ${FIPS202_SRCS})
add_library(aes256ctr_ref ${AES256CTR_SRCS})
add_library(sha2_ref ${SHA2_SRCS})

# Kyber 512
add_library(kyber512_ref ${KYBER_FIPS202_SRCS})
add_library(kyber512_90s_ref ${KYBER_NINETIES_SRCS})
target_compile_definitions(kyber512_ref PUBLIC KYBER_K=2)
target_compile_definitions(kyber512_90s_ref PUBLIC KYBER_K=2 KYBER_90S)
target_link_libraries(kyber512_ref INTERFACE fips202_ref)
target_link_libraries(kyber512_90s_ref INTERFACE aes256ctr_ref sha2_ref)

add_executable(test_kyber512_ref ${TEST_KYBER_SRCS})
add_executable(test_kex512_ref ${TEST_KEX_SRCS})
add_executable(test_vectors512_ref ${TEST_VECTORS_SRCS})
add_executable(test_kyber512-90s_ref ${TEST_KYBER_SRCS})
add_executable(test_kex512-90s_ref ${TEST_KEX_SRCS})
add_executable(test_vectors512-90s_ref ${TEST_VECTORS_SRCS})
target_link_libraries(test_kyber512_ref kyber512_ref)
target_link_libraries(test_kex512_ref kyber512_ref)
target_link_libraries(test_vectors512_ref kyber512_ref)
target_link_libraries(test_kyber512-90s_ref kyber512_90s_ref)
target_link_libraries(test_kex512-90s_ref kyber512_90s_ref)
target_link_libraries(test_vectors512-90s_ref kyber512_90s_ref)

# Kyber 768
add_library(kyber768_ref ${KYBER_FIPS202_SRCS})
add_library(kyber768_90s_ref ${KYBER_NINETIES_SRCS})
target_compile_definitions(kyber768_ref PUBLIC KYBER_K=3)
target_compile_definitions(kyber768_90s_ref PUBLIC KYBER_K=3 KYBER_90S)
target_link_libraries(kyber768_ref INTERFACE fips202_ref)
target_link_libraries(kyber768_90s_ref INTERFACE aes256ctr_ref sha2_ref)

add_executable(test_kyber768_ref ${TEST_KYBER_SRCS})
add_executable(test_kex768_ref ${TEST_KEX_SRCS})
add_executable(test_vectors768_ref ${TEST_VECTORS_SRCS})
add_executable(test_kyber768-90s_ref ${TEST_KYBER_SRCS})
add_executable(test_kex768-90s_ref ${TEST_KEX_SRCS})
add_executable(test_vectors768-90s_ref ${TEST_VECTORS_SRCS})
target_link_libraries(test_kyber768_ref kyber768_ref)
target_link_libraries(test_kex768_ref kyber768_ref)
target_link_libraries(test_vectors768_ref kyber768_ref)
target_link_libraries(test_kyber768-90s_ref kyber768_90s_ref)
target_link_libraries(test_kex768-90s_ref kyber768_90s_ref)
target_link_libraries(test_vectors768-90s_ref kyber768_90s_ref)

# Kyber 1024
add_library(kyber1024_ref ${KYBER_FIPS202_SRCS})
add_library(kyber1024_90s_ref ${KYBER_NINETIES_SRCS})
target_compile_definitions(kyber1024_ref PUBLIC KYBER_K=4)
target_compile_definitions(kyber1024_90s_ref PUBLIC KYBER_K=4 KYBER_90S)
target_link_libraries(kyber1024_ref INTERFACE fips202_ref)
target_link_libraries(kyber1024_90s_ref INTERFACE aes256ctr_ref sha2_ref)

add_executable(test_kyber1024_ref ${TEST_KYBER_SRCS})
add_executable(test_kex1024_ref ${TEST_KEX_SRCS})
add_executable(test_vectors1024_ref ${TEST_VECTORS_SRCS})
add_executable(test_kyber1024-90s_ref ${TEST_KYBER_SRCS})
add_executable(test_kex1024-90s_ref ${TEST_KEX_SRCS})
add_executable(test_vectors1024-90s_ref ${TEST_VECTORS_SRCS})
target_link_libraries(test_kyber1024_ref kyber1024_ref)
target_link_libraries(test_kex1024_ref kyber1024_ref)
target_link_libraries(test_vectors1024_ref kyber1024_ref)
target_link_libraries(test_kyber1024-90s_ref kyber1024_90s_ref)
target_link_libraries(test_kex1024-90s_ref kyber1024_90s_ref)
target_link_libraries(test_vectors1024-90s_ref kyber1024_90s_ref)

add_test(NAME kyber512_ref COMMAND test_kyber512_ref)
add_test(NAME kex512_ref COMMAND test_kex512_ref)
add_test(NAME kyber512-90s_ref COMMAND test_kyber512-90s_ref)
add_test(NAME kex512-90_ref COMMAND test_kex512-90s_ref)
add_test(NAME kyber768_ref COMMAND test_kyber768_ref)
add_test(NAME kex768_ref COMMAND test_kex768_ref)
add_test(NAME kyber768-90s_ref COMMAND test_kyber768-90s_ref)
add_test(NAME kex768-90_ref COMMAND test_kex768-90s_ref)
add_test(NAME kyber1024_ref COMMAND test_kyber1024_ref)
add_test(NAME kex1024_ref COMMAND test_kex1024_ref)
add_test(NAME kyber1024-90s_ref COMMAND test_kyber1024-90s_ref)
add_test(NAME kex1024-90_ref COMMAND test_kex1024-90s_ref)

if(WIN32)
  add_test(NAME vectors512_ref COMMAND PowerShell -Command "$<TARGET_FILE:test_vectors512_ref> | dos2unix > tvecs512")
  add_test(NAME vectors512-90s_ref COMMAND PowerShell -Command "$<TARGET_FILE:test_vectors512-90s_ref> | dos2unix > tvecs512-90s")
  add_test(NAME vectors768_ref COMMAND PowerShell -Command "$<TARGET_FILE:test_vectors768_ref> | dos2unix > tvecs768")
  add_test(NAME vectors768-90s_ref COMMAND PowerShell -Command "$<TARGET_FILE:test_vectors768-90s_ref> | dos2unix > tvecs768-90s")
  add_test(NAME vectors1024_ref COMMAND PowerShell -Command "$<TARGET_FILE:test_vectors1024_ref> | dos2unix > tvecs1024")
  add_test(NAME vectors1024-90s_ref COMMAND PowerShell -Command "$<TARGET_FILE:test_vectors1024-90s_ref> | dos2unix > tvecs1024-90s")
  add_test(NAME debug COMMAND powershell -Command "gc tvecs512 | select -first 10")
else()
  add_test(NAME vectors512_ref COMMAND sh -c "\"$<TARGET_FILE:test_vectors512_ref>\" > tvecs512")
  add_test(NAME vectors512-90s_ref COMMAND sh -c "\"$<TARGET_FILE:test_vectors512-90s_ref>\" > tvecs512-90s")
  add_test(NAME vectors768_ref COMMAND sh -c "\"$<TARGET_FILE:test_vectors768_ref>\" > tvecs768")
  add_test(NAME vectors768-90s_ref COMMAND sh -c "\"$<TARGET_FILE:test_vectors768-90s_ref>\" > tvecs768-90s")
  add_test(NAME vectors1024_ref COMMAND sh -c "\"$<TARGET_FILE:test_vectors1024_ref>\" > tvecs1024")
  add_test(NAME vectors1024-90s_ref COMMAND sh -c "\"$<TARGET_FILE:test_vectors1024-90s_ref>\" > tvecs1024-90s")
endif()

add_test(NAME hashes COMMAND sha256sum -c ../../SHA256SUMS)
